/*
 Copyright (C) 2007 - Michael Opitz
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#ifndef MY_SSL_H
#define MY_SSL_H

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <stdio.h>
#include <malloc.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <assert.h>
#include <unistd.h>


/** 
 * This structure represents the ssl-system.
 */
typedef struct {
	SSL_CTX *_ctx;
} ssl_system;

/** 
 * This structure represents a ssl-socket.
 */
typedef struct {
	int _sock;
	SSL *_ssl;
} ssl_socket;

/** 
 * init_ssl:
 *
 * This function inits the ssl-system.
 * 
 * Returns: a pointer to a ssl_system object.
 */
ssl_system* 
init_ssl ();
/** 
 * free_ssl
 *
 * This function cleans the ssl_system up and frees all allocated memory.
 * 
 * @sys is the ssl-system which should be freed.
 */
void 
free_ssl (ssl_system *sys);

/** 
 * close_socket:
 * 
 * @sys: is a pointer to a ssl-system
 * @sock: is a pointer to a socket
 *
 * This function closes a ssl_socket.
 *
 */
void 
close_socket (ssl_system *sys, ssl_socket *sock);
/** 
 * open_socket:
 * 
 * @sys: is a pointer to a ssl-system.
 * @hostname: is a string which contains the hostname.
 * @port: is the port to which the socket should connect.
 * 
 * This function opens a new ssl-socket.
 *
 * Returns: a pointer to an opened ssl-socket
 */
ssl_socket* 
open_socket (ssl_system *sys, const char *hostname, int port);
/** 
 * write_sock:
 * 
 * @sys: a pointer to a ssl-system
 * @sock: a pointer to a ssl-socket
 * @msg: a char-array
 *
 * This function sends a message over a ssl-socket.
 *
 */
void 
write_sock (ssl_system *sys, ssl_socket *sock, const char *msg);
/** 
 * read_sock:
 * 
 * @sys: is a pointer to a ssl-system
 * @sock: is a pointer to a ssl-socket
 * @buff: is the buffer in which should be written
 * @size: is the size of the buffer
 *
 * This function receives a message from the ssl-socket and stores it into buff.
 * 
 * Returns:   
 */
int 
read_sock (ssl_system *sys, ssl_socket *sock, char *buff, int size);

#endif
